<?php
namespace app\home\controller;
use think\Controller;
use think\Loader;
use think\request;
use think\Session;
use think\Cookie;

class Login extends Common
{
	public function _initialize(){
		
	}
	
   	public function sendSms()
    {
        $mobile = request()->post('mobile');
        $user=db('users')->where("tel='{$mobile}'")->find();
         
        if(!$user){
            return array('status' =>-1,'message'=>"号码不存在" );
        }
        if ( $user['openid'] )
        {
        	 return array('status' =>-1,'message'=>"手机号已绑定" );
        }

        $url= url('sms/send','','',true);
        // die($url);
        $param = array('type' =>'login','mobile' =>$mobile);
        $result=http_post($url,$param);
        $result=json_decode($result,1);
        // print_r($result);die;
        if($result['status']==1){
            Session::set('code',$result['message']);

            $result['message']='验证码已发送';//.$result['message']
        }
        return $result;
    }    

	public function login(){
        if (request()->isPost()) {
            $username = request()->post('username');
            $pwd = request()->post('pwd');
            
            $user=db('users')->where("username='{$username}'")->find();

            if(!$user){
                return $result=['status'=>0,'message'=>'用户名不存在'];
            }else{
				
				if($user['pwd'] != md5($pwd)){
					return $result=['status'=>0,'message'=>'密码错误!'];
				}
			}
			

			$map['last_login'] = date("Y-m-d H:i:s");
			$map['last_ip'] = $this->request->ip();;
			db('users')->where('id', $user['id'])->update($map);
            session('home_uid',$user['id']);
            $result=['status'=>1,'message'=>'登录成功','url'=>url('/home/index')];
            return $result;
        }
        return $this->fetch();
    }

    public function logout(){
		session::delete('home_uid');
		echo 0;
	}	

    
	public function index()
	{
        /*if(session('home_uid')){
            $this->redirect(url('index'));
        }*/
		return view();
	}

	public function register(){
		
		if (request()->isPost()) {
            $username = request()->post('username');
            $pwd = request()->post('pwd');
            $mobile = request()->post('mobile');
            $invent_id = request()->post('invent_id');
            $user=db('users')->where("username='{$username}'")->find();

            if($user){
                return $result=['status'=>0,'message'=>'用户名已存在!'];
            }else{
				$tmp = db('users')->where('invint_id', $invent_id)->find();
				if(!$tmp){
					return $result=['status'=>0,'message'=>'邀请码不存在!'];
				}else{
					$parent_id  = $tmp['id'];
				}
				if($tmp['level'] == 0){
					return $result=['status'=>0,'message'=>'【推荐码】没有分销权限!'];
				}
			}
			$data['username'] = $username;
			$data['pwd'] = md5($pwd);
			$data['parent_id'] = $parent_id;
			$data['mobile'] = $mobile;

			if($user['f_p_ids']){
				$f_p_ids = $user['f_p_ids'].",".$parent_id;
			}else{
				$f_p_ids = $parent_id;
			}
			$data['f_p_ids'] = $f_p_ids;
			//生成随机推荐码
			$rand_code = substr(md5($username.$pwd), 0, 5);
			$data['invint_id'] = $rand_code;
			$data['reg_time'] = date("Y-m-d H:i:s");
			db('users')->insert($data);
			$result=['status'=>1,'message'=>'注册成功!','url'=>url('/home/login/index')];
            return $result;
        }
		$this->assign('invint_id', input("param.invint_id"));
	    return $this->fetch();
	}
   

}